﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using TicTacToe.Core;

namespace TicTacToe.UI
{
    internal partial class TurnListWindow : Window
    {
        struct TWrapper
        {
            public Turn turn;

            public int Index { get; set; }

            public Brush PlayerColor { get { return turn.Player.Color.CreateBrush(); } set { } }
            public string PlayerName { get { return turn.Player.Name; } set { } }
            public string TurnPosition { get { return turn.ToString(); } set { } }
        }

        private readonly Stack<Turn> _turns;

        public TurnListWindow(IEnumerable<Turn> turns)
        {
            InitializeComponent();

            _turns = new Stack<Turn>();
            foreach (var t in turns)
                TurnHandler(null, new TurnEventArgs(t));
        }

        public void TurnHandler(object sender, TurnEventArgs e)
        {
            Action<Turn> a = t =>
                                {
                                    _turns.Push(t);
                                    var tw = new TWrapper { turn = t, Index = _turns.Count };
                                    turnListBox.Items.Add(tw);
                                    turnListBox.ScrollIntoView(tw);
                                };
            Dispatcher.Invoke(a, e.Turn);
        }
    }
}